﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Hr.BLL;
using Hr.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HR.HrHelper;
using System.Web.Http.Controllers;
using Hr.IBLL;
using Hr.BLLContainer;

namespace HR.Areas.letao.Controllers.Menu
{
    public class MenuMangeController : ApiController
    {
        private MenuService MenuService = new MenuService();
        private ISys_MenuService IMenu = Container.Resolve<ISys_MenuService>();
        int code = 0;
        string msg = "success";

        #region 菜单管理API
        #region 获取数据

        /// <summary>
        /// 获取所有菜单信息，模糊查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetMenuList(string Term)
        {
            if (Term == null)
            {
                Term = "";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",MenuService.GetMenuList(Term)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 获取菜单类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetTypeList()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",MenuService.GetTypeList()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据ID查询菜单信息
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetMenuInfo(int MenuID)
        {
            Sys_Menu sMenu = new Sys_Menu();
            if (MenuID > 0)
            {
                sMenu = MenuService.GetMenuInfo(MenuID);
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",sMenu}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 获取用户权限菜单信息
        /// </summary>
        /// <param name="pk_User"></param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetUserMenuList(int pk_User)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",MenuService.GetUserMenuList(pk_User)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 修改、添加
        /// <summary>
        /// 修改、添加、删除菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateMenu()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            string type = RequestForm["Type"];
            Sys_Menu menu = JsonHelper.DeserializeJsonToObject<Sys_Menu>(RequestForm["MenuInfo"]);//信息前后端对接无误
            if (type.Equals("Edit"))
            {

                Sys_Menu SelMenu = MenuService.GetMenuInfo(menu.pk_Menu);//更新后再获取数据无效，是老数据，考虑bug是ioc注入问题
                msg = "更新成功";
                if (SelMenu == null)
                {
                    code = 1;
                    msg = "菜单不存在";
                }
                else
                {
                    //两个对象对比是否相等
                    if (ModelHelper.ReflectModel(menu, SelMenu))
                    {
                        msg = "无任何更新";
                    }
                    else
                    {
                        if (!MenuService.UpdateMenu(menu, type))
                        {
                            code = 1;
                            msg = "更新失败";
                        }
                    }
                }
            }
            else
            {
                msg = "添加成功";
                if (!MenuService.UpdateMenu(menu, type))
                {
                    code = 1;
                    msg = "添加失败";
                }
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject DelMenu()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_Menu = int.Parse(RequestForm["pk_Menu"].ToString());

            msg = "删除成功";
            Sys_Menu sMenu = MenuService.GetMenuInfo(pk_Menu);
            if (sMenu != null)
            {
                if (!MenuService.DelMenu(pk_Menu))
                {
                    code = 1;
                    msg = "删除失败";
                }
            }
            else
            {
                code = 1;
                msg = "菜单不存在";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        #endregion
        #endregion


    }
}

